﻿#region

using System.Linq.Expressions;
using SqlSugar;
using Vin.Extension.SqlSugar.SugarFilter;

#endregion

namespace Vin.Extension.SqlSugar.Repository;

// [VinRepository]
public class VinBaseLogicRepository<TEntity> : VinBaseRepository<TEntity>, IVinBaseLogicRepository<TEntity>
    where TEntity : class, IDeleted, new()
{
    public virtual IUpdateable<TEntity> Updateable(Expression<Func<TEntity, TEntity>> columns,
        Expression<Func<TEntity, bool>> whereExpression)
    {
        return this.Context.Updateable<TEntity>()
            .SetColumns(columns, true) //true 支持更新数据过滤器赋值字段一起更新
            .Where(whereExpression);
    }

    public virtual IUpdateable<TEntity> UpdateableEnableQueryFilter(Expression<Func<TEntity, TEntity>> columns,
        Expression<Func<TEntity, bool>> whereExpression)
    {
        return this.Context.Updateable<TEntity>()
            .SetColumns(columns, true) //true 支持更新数据过滤器赋值字段一起更新
            .EnableQueryFilter()
            .Where(whereExpression);
    }

    public virtual IUpdateable<TEntity> Updateable(TEntity entity, Expression<Func<TEntity, bool>> whereExpression,
        bool ignoreAllNullColumns = true)
    {
        return this.Context.Updateable(entity)
            .IgnoreColumns(ignoreAllNullColumns)
            .Where(whereExpression);
    }

    public virtual Task<bool> ExecuteUpdateableEnableQueryFilterAsync(Expression<Func<TEntity, TEntity>> columns,
        Expression<Func<TEntity, bool>> whereExpression)
    {
        return UpdateableEnableQueryFilter(columns, whereExpression).ExecuteCommandHasChangeAsync();
    }

    public virtual Task<bool> ExecuteUpdateableAsync(TEntity entity, Expression<Func<TEntity, bool>> whereExpression,
        bool ignoreAllNullColumns = true)
    {
        return Updateable(entity, whereExpression, ignoreAllNullColumns).ExecuteCommandHasChangeAsync();
    }

    public IUpdateable<TEntity> DeleteUpdateable(Expression<Func<TEntity, bool>> whereExpression)
    {
        return Updateable(it => new TEntity() { IsDeleted = true }, whereExpression);
    }

    public IUpdateable<TEntity> UnDeleteUpdateable(Expression<Func<TEntity, bool>> whereExpression)
    {
        return Updateable(it => new TEntity() { IsDeleted = false }, whereExpression);
    }

    /// <summary>
    /// 逻辑删除
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <typeparam name="T"></typeparam>
    public bool LogicDelete(Expression<Func<TEntity, bool>> whereExpression)
    {
        // return this.Context.Updateable<TEntity>()
        //     .SetColumns(it => new TEntity() { IsDeleted = true },
        //         true) //true 支持更新数据过滤器赋值字段一起更新
        //     .Where(whereExpression).ExecuteCommandHasChange();
        return DeleteUpdateable(whereExpression).ExecuteCommandHasChange();
    }

    public async Task<bool> LogicDeleteAsync(Expression<Func<TEntity, bool>> whereExpression)
    {
        // return await this.Context.Updateable<TEntity>()
        //     .SetColumns(it => new TEntity() { IsDeleted = true },
        //         true) //true 支持更新数据过滤器赋值字段一起更新
        //     .Where(whereExpression).ExecuteCommandHasChangeAsync();
        return await DeleteUpdateable(whereExpression).ExecuteCommandHasChangeAsync();
    }

    public bool UnDelete(Expression<Func<TEntity, bool>> whereExpression)
    {
        return UnDeleteUpdateable(whereExpression).ExecuteCommandHasChange();
    }

    public async Task<bool> UnDeleteAsync(Expression<Func<TEntity, bool>> whereExpression)
    {
        return await UnDeleteUpdateable(whereExpression).ExecuteCommandHasChangeAsync();
    }
}